#!/usr/bin/env python
# -*- coding: utf-8 -*-
from common import L
from common.tools import shell
import sys

__author__ = 'Administrator'


class Environment:
    def __init__(self):
        """用于检测框架的运行环境
        """
        self.check_python()
        self.check_depandent_module()

    def check_python(self):
        __version__ = sys.version_info
        L.info('python Version: {}.{}.{}'.format(__version__[0], __version__[1], __version__[2]))

    def check_depandent_module(self):
        L.info('check dependent modules are installed')
        self.requests_v()
        self.pytest_v()
        self.pytest_html_v()
        self.click_v()
        self.yaml_v()
        self.selenium_v()
        self.psutil_v()
        self.pywin32_v()
        self.jinja2_v()

    def requests_v(self):
        try:
            __v__ = shell('python -m pip show requests').splitlines()[1]
            L.info('requests {}'.format(__v__))
        except IndexError as e:
            L.error('check requests failed:{}'.format(e))

    def pytest_v(self):
        try:
            __v__ = shell('python -m pip show pytest').splitlines()[1]
            L.info('pytest {}'.format(__v__))
        except IndexError as e:
            L.error('check pytest failed: {}'.format(e))

    def pytest_html_v(self):
        try:
            __v__ = shell('python -m pip show pytest-html').splitlines()[1]
            L.info('pytest-html {}'.format(__v__))
        except IndexError as e:
            L.error('check pytest-html failed: {}'.format(e))

    def click_v(self):
        try:
            __v__ = shell('python -m pip show click').splitlines()[1]
            L.info('click {}'.format(__v__))
        except IndexError as e:
            L.error('check click failed: {}'.format(e))

    def yaml_v(self):
        try:
            __v__ = shell('python -m pip show pyyaml').splitlines()[1]
            L.info('pyyaml {}'.format(__v__))
        except IndexError as e:
            L.error('check pyyaml failed: {}'.format(e))

    def selenium_v(self):
        try:
            __v__ = shell('python -m pip show selenium').splitlines()[1]
            L.info('selenium {}'.format(__v__))
        except IndexError as e:
            L.error('check selenium failed: {}'.format(e))

    def psutil_v(self):
        try:
            __v__ = shell('python -m pip show psutil').splitlines()[1]
            L.info('psutil {}'.format(__v__))
        except IndexError as e:
            L.error('check psutil failed: {}'.format(e))

    def pywin32_v(self):
        try:
            __v__ = shell('python -m pip show pywin32').splitlines()[1]
            L.info('pywin32 {}'.format(__v__))
        except IndexError as e:
            L.error('check pywin32 failed: {}'.format(e))

    def jinja2_v(self):
        try:
            __v__ = shell('python -m pip show jinja2').splitlines()[1]
            L.info('jinja2 {}'.format(__v__))
        except IndexError as e:
            L.error('check jinja2 failed: {}'.format(e))

if __name__ == '__main__':
    Environment()
